package io.github.loserYa.mutil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.env.Environment;

/**
 * mq 多数据源工厂
 *
 * @author loser
 * @date 2024/4/25
 */
public class RabbitFactory extends AbstractFactory {

    private static final Logger logger = LoggerFactory.getLogger(RabbitFactory.class);

    public RabbitFactory(String pre, ConfigurableListableBeanFactory factory, Environment environment) {

        logger.info(pre + "RabbitFactory init start");
        String host = environment.getProperty("spring.rabbitmq." + pre + ".host");
        Integer port = environment.getProperty("spring.rabbitmq." + pre + ".port", Integer.class);
        String username = environment.getProperty("spring.rabbitmq." + pre + ".username");
        String password = environment.getProperty("spring.rabbitmq." + pre + ".password");
        String virtualHost = environment.getProperty("spring.rabbitmq." + pre + ".virtualHost");
        String module = environment.getProperty("spring.rabbitmq." + pre + ".listener.simple.acknowledge-mode");
        logger.info(pre + "RabbitFactory args host:{} port:{} username:{} password:{} virtualHost:{} module:{}", host, port, username, password, virtualHost, module);

        ConnectionFactory connectionFactory = buildConnectionFactory(host, port, username, password, virtualHost);
        factory.registerSingleton(pre + "ConnectionFactory", connectionFactory);
        factory.registerSingleton(pre + "ContainerFactory", buildContainerFactory(connectionFactory, module));
        factory.registerSingleton(pre + "RabbitTemplate", buildRabbitTemplate(connectionFactory));
        factory.registerSingleton(pre + "RabbitAdmin", buildRabbitAdmin(connectionFactory));
        logger.info(pre + "ConnectionFactory init finish");
        logger.info(pre + "ContainerFactory init finish");
        logger.info(pre + "RabbitTemplate init finish");
        logger.info(pre + "RabbitAdmin init finish" + System.lineSeparator());

    }

}
